package com.example.javabasic.word;


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 生成word文档工具
 * @Author Lin FuYuan
 * @date 2021/6/28
 */
public class CreateWordFile {
    public static void main(String[] args) {
        String ftlPath = "I:\\IDEA\\workspace\\java-basic\\src\\main\\java\\com\\example\\javabasic\\word\\model";
        String ftlName = "xiangxisheji.xml";
//        String ftlName = "test.ftl";
        String wordPath = "C:\\Users\\Administrator\\Desktop\\朗擎系统文档\\生成word结果";
        String wordName = "测试生成文档.doc";
        Map<String,Object> map = new HashMap<>(16);
        map.put("hello","6666666666666666666666");
//        createWordForFtl(ftlPath,ftlName,wordPath,wordName, Arrays.asList(map,map));
        System.out.println( BigDecimal.class.isPrimitive());
        System.out.println(int.class.isPrimitive());
        System.out.println(void.class.isPrimitive());
        System.out.println(Class.class.isPrimitive());
        System.out.println(Integer.class.isPrimitive());

    }

    /**
     *
     * @author Lin FuYuan
     * @param ftlPath ftl文件路径
     * @param ftlName ftl 文件名
     * @param wordPath word 生成后存储路径
     * @param wordName word 生成后名称
     * @param dataMap 要转换的数据map
     * @date 2021/6/28
     * @return void
     */
    public static void  createWordForFtl(String ftlPath, String ftlName, String wordPath, String wordName, List<Map<String,Object>> dataMap){

        //Configuration 用于读取ftl文件模板
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
        configuration.setDefaultEncoding("UTF-8");
        try {
            //此处设置模板存储路径
            configuration.setDirectoryForTemplateLoading(new File(ftlPath));
        } catch (IOException e) {
            System.out.println("找不到模板文件");
            e.printStackTrace();
        }
        //构建模板
        Template t = null;
        try {
            //此处根据模板名称读取文件
            t = configuration.getTemplate(ftlName);
        } catch (IOException e) {
            System.out.println("ftl 文件不存在");
            e.printStackTrace();
        }
        //创建目录
        createDirs(wordPath);
        //文件生成
        File file  = new File(wordPath+"\\"+wordName);
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
            Map<String,Object> dataList = new HashMap<>();
            dataList.put("dataList",dataMap);
            t.process(dataList,out);
        } catch (UnsupportedEncodingException e) {
            System.out.println("不支持的类型");
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.out.println("找不到文件");
            e.printStackTrace();
        } catch (TemplateException e) {
            System.out.println("模板异常");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("io异常");
            e.printStackTrace();
        } finally{
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(file!=null){
                file.setWritable(true);
            }
        }
    }

    /**
     * 生成目录
     */
    public static void createDirs(String filePath){
        File file = new File(filePath);
        if(!file.exists()){
            file.mkdirs();
        }
    }
}
